﻿using System.Configuration;
using SqlSugar;
using SqlSugarTests.Comm;

namespace SqlSugarTests.Models
{
    public class SqlSugarConfig
    {
        public SqlSugarClient GetInstance()
        {
            string connString = ConfigurationManager.ConnectionStrings["Default"].ToString();
            SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = connString,
                DbType = DbType.SqlServer,
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute,
            });
            return db;
        }

        public SqlSugarClient GetInstance(string connStringOrKey, ConnStringType connType)
        {
            string connString = string.Empty;
            if (connType == ConnStringType.Key)
            {
                connString = ConfigurationManager.ConnectionStrings[connStringOrKey].ToString();
            }
            else
            {
                connString = connStringOrKey;
            }
            SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = connString,
                DbType = DbType.SqlServer,
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute,
            });
            return db;
        }


        public SqlSugarConfig()
        {
            this.Db = GetInstance();
        }

        /// <summary>
        /// 初始化Db
        /// </summary>
        /// <param name="connStringOrKey">连接字符串或者Key值</param>
        /// <param name="connType">连接字符串类型</param>
        public SqlSugarConfig(string connStringOrKey, ConnStringType connType)
        {
            this.Db = GetInstance(connStringOrKey, connType);
        }


        public SqlSugarClient Db { get; set; }

        public DbSet<Student> Students { get { return new DbSet<Student>(Db); } }

        public DbSet<StudentExtend> StudentExtends { get { return new DbSet<StudentExtend>(Db); } }

        public DbSet<User_Role> User_Role
        {
            get { return new DbSet<User_Role>(Db); }
        }
    }
}